/**
 * Toort Scripting by Alex Vanyan (alex [at] toort [dot] net).
 * (c) 2011 Barsamini Toort (http://toort.net).
 */

;(function($) {

	$.infiniteSlider = { version: '1.0', props: [] };
	
	var defaults = {
		lines: 1,
        speed: 10,
        margin: 10,
        slideSelector: "slide",
        slideColorSelector: "slide-color",
        colors: []
	};

	var $this, o, tmpObj, tmpVar;
    var additionalQuantity = 2;
    var pointerStack = [];

	$.fn.infiniteSlider = function(options) {
		return this.each(function() {
			o = $.extend(defaults, options);
			$this = $(this);
            var lock = -1;
            o.margin = parseInt(o.margin);
            if(!o.colors || o.colors.constructor.toString().match(/Array/) === null || o.colors.length < 1) return false;
			if(o.lines < 0) o.lines = -parseInt(o.lines);
            tmpVar = $(".slider-element", $this).get(0).clientWidth;
            var slideClasses = $("." + o.slideSelector).get(0).className;
            var slideColorClasses = $("." + o.slideColorSelector).get(0).className;
            var screenMaxCapacity = parseInt(screen.width / (tmpVar + o.margin)) + additionalQuantity;
            $(".slider-element").remove();
            for(var ln = 1; ln <= o.lines; ln++) {
                $this.append('<div id="slider-section-' + ln + '" class="slider-section ' + (ln % 2 == 0 ? 'slider-even-line' : 'slider-odd-line') + '"><div id="slider-sliding-bar-' + ln + '" class="slider-sliding-bar" style="margin-left: ' + (ln % 2 ? '0' : '-' + (tmpVar + o.margin)) + 'px; width: ' + (screen.width + additionalQuantity * (tmpVar + o.margin)) + 'px;"></div></div>');
                for(var it = 0; it < screenMaxCapacity; it++) {
                    tmpLock = parseInt(Math.random() * o.colors.length);
					if(it == 0) lockFirst = tmpLock;
                    if(tmpLock != lock || (it == screenMaxCapacity && tmpLock == lockFirst)) {
                        lock = tmpLock;
                        randIndex = parseInt(Math.random() * o.colors[lock].length);
                        $('#slider-sliding-bar-' + ln, $this).append('<div id="slider-slide-' + (it + 1) + '" class="' + slideClasses + '" style="margin-right: ' + o.margin + 'px;"><span style="background-color: #' + o.colors[lock][randIndex] + ';" class="' + slideColorClasses + '"></span></div>');
                    } else {
                        it--;
                        continue;
                    }
                }
                pointerStack[ln - 1] = false;
                animateSlider(ln);
            }
			/* *** IN DEVELOPMENT STAGE ***
            $("." + o.slideSelector).bind("mouseover", function() {
                pointerStack[$this.children().get(0).indexOf($(this).parent().get(0))] = true;
                console.log($this.children().get(0).indexOf($(this).parent().get(0)))
            });
            $("." + o.slideSelector).bind("mouseout", function() {
                pointerStack[$this.children().index($(this).parent().get(0))] = false;
            });
			*/
		});
	};

    var animateSlider = function(sliderTabId) {
        var slider = document.getElementById("slider-sliding-bar-" + sliderTabId);
        var step = 1;
        if(pointerStack[sliderTabId] !== true) {
			if(sliderTabId % 2) {
				if(parseInt(slider.style.marginLeft) <= -(tmpVar + o.margin)) {
					slider.style.marginLeft = "0px";
					slider.appendChild(slider.children[0]);
				} else {
					slider.style.marginLeft = (parseInt(slider.style.marginLeft) - step) + "px";
				}
			} else {
				if(parseInt(slider.style.marginLeft) >= 0) {
					slider.style.marginLeft = "-" + (tmpVar + o.margin) + "px";
					slider.insertBefore(slider.children[slider.children.length - 1], slider.children[0]);
				} else {
					slider.style.marginLeft = (parseInt(slider.style.marginLeft) + step) + "px";
				}
			}
		}
        window.setTimeout(function() {
            animateSlider(sliderTabId);
        }, parseInt(o.speed));
    };

    var arrayLen = function(arrayObj, arrLen) {
        if(typeof arrLen === "undefined") arrLen = 0;
        for(var i in arrayObj) {
            if(arrayObj[i].constructor.toString().match(/Array/)) {
                tmp__ = arrayLen(arrayObj[i], arrLen);
                if(tmp__ > arrLen) arrLen = tmp__;
            } else {
                arrLen++;
            }
        }
        return arrLen;
    };

})(jQuery);
